<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>操作员管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="../../layuimini/lib/layui-v2.5.5/css/layui.css" media="all" />
    <link rel="stylesheet" href="../../layuimini/css/public.css" media="all" />
  </head>

  <body>
    <div class="layuimini-container">
      <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
          <div class="layui-btn-container">
            <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add">添加</button>
            <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</button>
            <button class="layui-btn layui-btn-sm layui-btn-sucess" lay-event="refresh">刷新</button>
          </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
      </div>
    </div>

    <script type="text/html" id="userEditDialog">
      <form id="userEditForm" lay-filter="userEditForm" class="layui-form layui-form-pane" style="padding-left:20px;margin-top: 20px;">
        <div class="layui-row">
          <div class="layui-col-md6">
            <div class="layui-form-item">
              <label class="layui-form-label layui-form-required">用户名:</label>
              <div class="layui-input-block">
                <input type="text" name="username" placeholder="请输入用户名" class="layui-input" lay-verType="tips" lay-verify="required" required autocomplete="off" />
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">密码:</label>
              <div class="layui-input-block">
                <input type="password" name="password" placeholder="请输入密码" class="layui-input" autocomplete="off" id="password1" />
              </div>
            </div>
            <div class="layui-form-item">
              <label class="layui-form-label">确认密码:</label>
              <div class="layui-input-block">
                <input type="password" name="password2" placeholder="请输入确认密码" class="layui-input" autocomplete="off" id="password2" />
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-inline">
                <label class="layui-form-label">用户角色:</label>
                <div class="layui-input-inline">
                  <select name="roleId" id="roleId" lay-filter="selectRole"></select>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="layui-form-item layui-input-block">
          <button class="layui-btn" lay-filter="userEditSubmit" lay-submit>提交</button>
          <button class="layui-btn layui-btn-primary" type="button" id="Cancel">取消</button>
        </div>
      </form>
    </script>

    <script type="text/html" id="currentTableBar">
      <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="edit">编辑</a>
      <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</a>
    </script>

    <script src="../../layuimini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="../../layuimini/js/lay-config.js" charset="utf-8"></script>

    <script>
      layui.use(["layer", "form", "table", "common"], function () {
        var $ = layui.jquery,
          form = layui.form,
          table = layui.table,
          common = layui.common;

        var tableIns = table.render({
          elem: "#currentTableId",
          url: common.api + "/user/list",
          where: { token: common.getToken },
          toolbar: "#toolbarDemo",
          title: "操作员信息",
          cols: [
            [
              {
                type: "checkbox",
                width: 50,
              },
              {
                field: "id",
                width: 80,
                title: "ID",
                align: "center",
                sort: true,
              },
              {
                field: "username",
                width: 250,
                title: "用户名",
                align: "center",
              },
              {
                field: "roleDescription",
                width: 250,
                title: "角色名称",
                align: "center",
              },
              {
                title: "操作",
                minWidth: 150,
                toolbar: "#currentTableBar",
                align: "center",
              },
            ],
          ],
          limits: [1, 5, 10, 15, 20],
          limit: 15,
          page: true,
          skin: "row",
          even: true,
          parseData: function (res) {
            if (res.code != 200) {
              common.login(res);
            }
            return {
              code: res.code, //解析接口状态
              msg: res.msg, //解析提示文本
              count: res.data.total, //解析数据长度
              data: res.data.list, //解析数据列表
            };
          },
          response: {
            statusCode: 200, //重新规定成功的状态码为 200，table 组件默认为 0
          },
          request: {
            pageName: "page", //页码的参数名称，默认：page
            limitName: "size", //每页数据量的参数名，默认：limit
          },
          initSort: {
            field: "id",
            type: "asc",
          },
        });

        //监听工具栏添加删除刷新
        table.on("toolbar(currentTableFilter)", function (obj) {
          if (obj.event === "add") {
            // 监听添加操作
            showEditModel();
          } else if (obj.event === "delete") {
            // 监听删除操作
            var data = table.checkStatus("currentTableId").data;
            if (data.length === 0) {
              layer.msg("请选择要删除的数据", { icon: 2 });
              return;
            }
            var ids = data.map(function (d) {
              return d.id;
            });
            doDel(ids);
          } else if (obj.event === "refresh") {
            tableIns.reload({
              url: common.api + "/user/list",
              method: "get",
            });
          }
        });

        //监听编辑删除
        table.on("tool(currentTableFilter)", function (obj) {
          if (obj.event === "edit") {
            showEditModel(obj.data);
          } else if (obj.event === "delete") {
            doDel([obj.data.id]);
          }
        });

        //显示添加弹窗
        function showEditModel(formData) {
          var idx = layer.open({
            type: 1,
            area: ["50%"],
            title: formData ? "编辑" : "添加",
            anim: 1,
            shade: 0.6,
            content: $("#userEditDialog").html(),
            success: function (layero, dIndex) {
              // 回显表单数据
              form.val("userEditForm", formData);

              $.each(selectData, function (index, item) {
                var option = new Option(item.description, item.id);
                $("#roleId").append(option);
              });
              var index = $("#roleId").val();
              if (formData) {
                index = formData.roleId;
                $("#roleId").val(formData.roleId);
              }
              form.render("select");

              // 表单提交事件
              form.on("submit(userEditSubmit)", function (data) {
                if (!formData) {
                  if (data.field.password == "") {
                    layer.msg("请输入密码", { icon: 2 });
                    return false;
                  }
                }
                if (data.field.password != data.field.password2) {
                  layer.msg("密码输入不一致", { icon: 2 });
                  $("#password1").val("");
                  $("#password2").val("");
                  return false;
                }
                common.ajax(
                  formData ? common.api + "/user/" + formData.id : common.api + "/user/register",
                  JSON.stringify(data.field),
                  function (res) {
                    if (res.code === 200) {
                      layer.close(dIndex);
                      layer.msg(res.msg, { icon: 1 });
                      table.reload("currentTableId");
                    } else {
                      layer.msg(res.msg, { icon: 2 });
                    }
                  },
                  formData ? "put" : "post",
                  function (data) {
                    layer.msg(data.msg, { icon: 2 });
                  }
                );
                return false;
              });

              $("#Cancel").click(function () {
                layer.close(idx);
              });
            },
          });
        }

        function doDel(ids) {
          layer.confirm(
            "确定要删除选中数据吗？",
            {
              skin: "layui-layer-admin",
              shade: 0.1,
            },
            function (i) {
              var index = layer.load(2);
              common.ajax(
                common.api + "/user",
                JSON.stringify(ids),
                function (res) {
                  if (res.code === 200) {
                    layer.close(index);
                    layer.msg(res.msg, { icon: 1 });
                    tableIns.reload();
                  } else {
                    layer.close(index);
                    layer.msg(res.msg, { icon: 2 });
                  }
                },
                "DELETE",
                function (data) {
                  console.log(data);
                  layer.close(index);
                  layer.msg(data.msg, { icon: 2, time: 1000 });
                  return;
                }
              );
              layer.close(i);
            }
          );
        }

        var selectData;
        layer.ready(function () {
          common.ajax(common.api + "/role/select", null, function (data) {
            selectData = data.data;
          });
        });
      });
    </script>
  </body>
</html>
